from rest_framework import serializers
from django.contrib.auth.models import User
from .models import (
    JenkinsServer, Project, Environment, JenkinsJob,
    JenkinsBuild, JenkinsAudit
)


class UserSerializer(serializers.ModelSerializer):
    """用户序列化器"""
    class Meta:
        model = User
        fields = ['id', 'username', 'first_name', 'last_name', 'email']


class JenkinsServerSerializer(serializers.ModelSerializer):
    """Jenkins服务器序列化器"""
    class Meta:
        model = JenkinsServer
        fields = [
            'id', 'name', 'url', 'username', 'api_token', 'description', 'is_active',
            'status', 'job_count', 'last_sync', 'last_check', 'created_at', 'updated_at'
        ]
        extra_kwargs = {
            'api_token': {'write_only': True}
        }


class JenkinsServerListSerializer(serializers.ModelSerializer):
    """Jenkins服务器列表序列化器"""
    class Meta:
        model = JenkinsServer
        fields = [
            'id', 'name', 'url', 'username', 'description', 'is_active',
            'status', 'job_count', 'last_sync', 'last_check', 'created_at', 'updated_at'
        ]


class ProjectSerializer(serializers.ModelSerializer):
    """项目序列化器"""
    class Meta:
        model = Project
        fields = ['id', 'name', 'code', 'description', 'is_active', 'created_at', 'updated_at']


class EnvironmentSerializer(serializers.ModelSerializer):
    """环境序列化器"""
    class Meta:
        model = Environment
        fields = ['id', 'name', 'code', 'description', 'is_active', 'created_at', 'updated_at']


class JenkinsJobSerializer(serializers.ModelSerializer):
    """Jenkins任务序列化器"""
    project_name = serializers.ReadOnlyField(source='project.name')
    environment_name = serializers.ReadOnlyField(source='environment.name')
    jenkins_server_name = serializers.ReadOnlyField(source='jenkins_server.name')
    job_type_display = serializers.ReadOnlyField(source='get_job_type_display')
    last_build_status_display = serializers.ReadOnlyField(source='get_last_build_status_display')

    class Meta:
        model = JenkinsJob
        fields = [
            'id', 'name', 'description', 'job_type', 'job_type_display',
            'project', 'project_name', 'environment', 'environment_name',
            'jenkins_server', 'jenkins_server_name', 'jenkins_job_name', 'jenkins_job_url',
            'gitlab_project_id', 'gitlab_project_name', 'last_build_number',
            'last_build_status', 'last_build_status_display', 'last_build_time',
            'config_json', 'is_active', 'created_at', 'updated_at'
        ]


class JenkinsJobListSerializer(serializers.ModelSerializer):
    """Jenkins任务列表序列化器"""
    project_name = serializers.ReadOnlyField(source='project.name')
    environment_name = serializers.ReadOnlyField(source='environment.name')
    jenkins_server_name = serializers.ReadOnlyField(source='jenkins_server.name')
    job_type_display = serializers.ReadOnlyField(source='get_job_type_display')
    last_build_status_display = serializers.ReadOnlyField(source='get_last_build_status_display')

    class Meta:
        model = JenkinsJob
        fields = [
            'id', 'name', 'description', 'job_type', 'job_type_display',
            'project', 'project_name', 'environment', 'environment_name',
            'jenkins_server', 'jenkins_server_name', 'jenkins_job_name',
            'gitlab_project_id', 'gitlab_project_name', 'config_json',
            'last_build_number', 'last_build_status', 'last_build_status_display',
            'last_build_time', 'is_active', 'created_at', 'updated_at'
        ]


class JenkinsBuildSerializer(serializers.ModelSerializer):
    """Jenkins构建记录序列化器"""
    job_name = serializers.ReadOnlyField(source='job.name')
    project_name = serializers.ReadOnlyField(source='job.project.name')
    environment_name = serializers.ReadOnlyField(source='job.environment.name')
    status_display = serializers.ReadOnlyField(source='get_status_display')

    class Meta:
        model = JenkinsBuild
        fields = [
            'id', 'job', 'job_name', 'project_name', 'environment_name',
            'build_number', 'status', 'status_display', 'duration',
            'start_time', 'end_time', 'parameters', 'triggered_by',
            'git_commit', 'git_branch', 'jenkins_build_url',
            'created_at', 'updated_at'
        ]


class JenkinsBuildListSerializer(serializers.ModelSerializer):
    """Jenkins构建记录列表序列化器"""
    job_name = serializers.ReadOnlyField(source='job.name')
    project_name = serializers.ReadOnlyField(source='job.project.name')
    environment_name = serializers.ReadOnlyField(source='job.environment.name')
    status_display = serializers.ReadOnlyField(source='get_status_display')

    class Meta:
        model = JenkinsBuild
        fields = [
            'id', 'job_name', 'project_name', 'environment_name',
            'build_number', 'status', 'status_display', 'duration',
            'start_time', 'end_time', 'triggered_by', 'git_commit',
            'git_branch', 'created_at'
        ]


class JenkinsAuditSerializer(serializers.ModelSerializer):
    """Jenkins操作审计序列化器"""
    user_username = serializers.ReadOnlyField(source='user.username', allow_null=True)
    project_name = serializers.ReadOnlyField(source='project.name', allow_null=True)
    environment_name = serializers.ReadOnlyField(source='environment.name', allow_null=True)
    jenkins_server_name = serializers.ReadOnlyField(source='jenkins_server.name', allow_null=True)
    jenkins_job_name = serializers.ReadOnlyField(source='jenkins_job.name', allow_null=True)
    operation_type_display = serializers.ReadOnlyField(source='get_operation_type_display')

    class Meta:
        model = JenkinsAudit
        fields = [
            'id', 'operation_type', 'operation_type_display', 'user', 'user_username',
            'project', 'project_name', 'environment', 'environment_name',
            'jenkins_server', 'jenkins_server_name', 'jenkins_job', 'jenkins_job_name',
            'result', 'details', 'error_message', 'created_at'
        ]


class JenkinsAuditListSerializer(serializers.ModelSerializer):
    """Jenkins操作审计列表序列化器"""
    user_username = serializers.ReadOnlyField(source='user.username', allow_null=True)
    project_name = serializers.ReadOnlyField(source='project.name', allow_null=True)
    environment_name = serializers.ReadOnlyField(source='environment.name', allow_null=True)
    operation_type_display = serializers.ReadOnlyField(source='get_operation_type_display')

    class Meta:
        model = JenkinsAudit
        fields = [
            'id', 'operation_type', 'operation_type_display', 'user_username',
            'project_name', 'environment_name', 'result', 'created_at'
        ]
